package com.xiangyang.pos.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiangyang.pos.R;
import com.xiangyang.pos.bean.CommodityDataBean;

import java.util.List;

/**
 * Created by Administrator on 2017/8/18 0018.
 *
 * 订单生成适配器（扫码展示商品）
 *
 * @author YeDalin
 *
 */

public class OrderRecyclerViewAdapter extends RecyclerView.Adapter<OrderRecyclerViewAdapter.ViewHolder> {

    private List<CommodityDataBean.CommodityData> list;
    private Context context;

//    构造器，获取数据和context
    public OrderRecyclerViewAdapter(List<CommodityDataBean.CommodityData> list,Context context) {
        this.list = list;
        this.context=context;
    }

//    加载布局
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_recycler_view, parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }



//    数据处理
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        if (list.size() != 0) {
            holder.tvProductName.setText(""+list.get(position).getGoods_name()+"/"
                    +list.get(position).getMeasurement()
                    +list.get(position).getUnit());
            holder.tvProductPrice.setText("单价：￥"+ list.get(position).getPrice() );
            holder.tvProductNumber.setText(list.get(position).getCount() + "");


            if(list.get(position).getFlag().equals("f")){
                holder.tvF.setVisibility(View.VISIBLE);
            }

            if(list.get(position).getCount()>list.get(position).getGoods_num()){
                holder.tvProductName.setTextColor(Color.rgb(255, 0, 0));
                holder.tvProductNumber.setTextColor(Color.rgb(255,0,0));
            }else {
                holder.tvProductName.setTextColor(Color.rgb(0, 0, 0));
                holder.tvProductNumber.setTextColor(Color.rgb(0,0,0));
            }

            holder.ivDelete.setOnClickListener(myClick(position));

            holder.tvAdd.setOnClickListener(myClick(position));
            holder.tvDec.setOnClickListener(myClick(position));
        }

    }

//    列表长度
    @Override
    public int getItemCount() {
        return list.size();
    }



    private View.OnClickListener myClick(final int position){
        final View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.iv_delete:
                        deleteDialog(position);
                        break;
                    case R.id.tv_add:
                        if(list.get(position).getCount()<list.get(position).getGoods_num()){
                            list.get(position).setCount(list.get(position).getCount()+1);
                            notifyDataSetChanged();
                            if(deleteData!=null){
                                deleteData.setAllMoney(position,false);//回调计算价格
                            }

                        }else {
                            noHaveCommodity();
                        }
                        break;
                    case R.id.tv_dec:
                        list.get(position).setCount(list.get(position).getCount()-1);
                        if(list.get(position).getCount()==0){
                            list.remove(position);
                        }
                        notifyDataSetChanged();
                        if(deleteData!=null){
                            deleteData.setAllMoney(position,false);//回调计算价格
                        }

                        break;
                }
            }
        };

        return listener;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvProductName;
        public TextView tvProductNumber;
        public TextView tvProductPrice;
        public ImageView ivDelete;

        public TextView tvDec;
        public TextView tvAdd;

        private TextView tvF;

        public ViewHolder(View itemView) {
            super(itemView);
            tvProductName = itemView.findViewById(R.id.tv_product_name);
            tvProductPrice = itemView.findViewById(R.id.tv_price);
            tvProductNumber = itemView.findViewById(R.id.tv_product_number);
            ivDelete = itemView.findViewById(R.id.iv_delete);

            tvDec=itemView.findViewById(R.id.tv_dec);
            tvAdd=itemView.findViewById(R.id.tv_add);

            tvF=itemView.findViewById(R.id.tv_f);
        }
    }





    private DeleteData deleteData;
    public void setDeleteData(DeleteData deleteData) {
        this.deleteData = deleteData;
    }
    //    回调接口
    public interface DeleteData {
        void setAllMoney(int position,boolean isDelete);
    }


    //    删除商品对话框
    private void deleteDialog(final int position){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setCancelable(false);
        builder.setTitle("确定要删除该商品");
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                list.remove(position);//删除商品
                notifyDataSetChanged();
                if(deleteData!=null){
                    deleteData.setAllMoney(position,true);//回调计算价格
                }
            }
        });
        builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                builder.setCancelable(true);
            }
        });
        builder.show();

    }

    //    达到商品上限对话框
    private void noHaveCommodity() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(false);
        builder.setTitle("该商品已达到上限");
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                builder.setCancelable(true);
            }
        });
        builder.show();
    }


}
